block operation
let say I have a function named foo,I want to make the statement console.log('done') can execute without wating for resolving fetch response.
function foo(){
console.log('hello world'); //1
var res = fetch.get(); //ajax //2
console.log('done'); //3
}
execution order: 1,2,3
In promised-based solution,I can do something like this
function foo(){
console.log('hello world'); //1
fetch.get().then((result)=>{
var res = result;
}); //ajax 2
console.log('done'); //3
}
so that the execution order would be 1,3,2
However, Once changed this in async-await pattern
async function foo(){
console.log('hello world'); //1
var res = await fetch.get(); //ajax 2
console.log('done'); //3
}
the console.log('done') executed only the response has received.
exeuction order: 1 2 3
How do I make it non-blocking?
if on node, use Node version 8. there is a feature bug which causes the engine to behave as you expect.
console.log('hello world'); const p = fetch.get(); console.log('done'); await p;
Обсуждают сегодня