tags: yield、await、async javascript asynchronous
function call(){
console.log('call funtion');
return new Promise(resolve=>{
setTimeout(function(){
console.log('call funtion timeout');
resolve('dd');
},1000);
});
}
function normalFunction() {
console.log('normalFunction');
return 'data'
}
// call(function(){
// console.log('callback');
// });
async function asynctest() {
console.log('start');
await call();
await normalFunction();
await new Promise(resolve=>{ console.log('wait result'); resolve()});
console.log('end');
}
asynctest();
Results of the
PS F:\zzj1026\Rnx\testEgg> node .\async.js
start
call funtion
call funtion timeout
normalFunction
wait result
end
PS F:\zzj1026\Rnx\testEgg>
Personal understanding of async means that this function is asynchronous. The keyword await is blocking (suspend the function and wait for the return result). If the return value is a promise, wait for the result of the resolve or reject of the promise. If it is the normal value returned by the function, directly Execute down
function call(){
console.log('call funtion');
return new Promise(resolve=>{
setTimeout(function(){
console.log('call funtion timeout');
resolve('dd');
},1000);
});
}
function normalFunction() {
console.log('normalFunction');
return 'data'
}
function* yieldFunc() {
console.log('start');
yield call();
yield normalFunction();
console.log('end');
}
let yieldData=yieldFunc();
let firstData=yieldData.next();
console.log(firstData);
firstData.value.then(function(data){
console.log(data);
});
yieldData.next();
console.log(yieldData);
Results of the
PS F:\zzj1026\Rnx\testEgg> node .\async.js
start
call funtion
{ value: Promise { <pending> }, done: false }
normalFunction
{}
call funtion timeout
dd
PS F:\zzj1026\Rnx\testEgg>
Personal understanding: every time the function executes to yield, it returns a yield blocking value {value: Promise {}, done: false }, then the value can be processed, and then if done is true, it means that the function has been executed. Up
Turn to: https://www.cnblogs.com/liqingwen/p/5831951.html Worship Going into the world of asynchronous programming - getting started with async/await sequence This is the introductory pa...
First, the advantages of async/await 1) Convenient cascading call:That is, calling scenes that occur sequentially; 2) Synchronization code writing method: Promise uses the then function to perform cha...
Async with await is proposed by ES7, its implementation is based on Promise. The code is easier to read by writing in a synchronous manner. The await function cannot be used alone, and the async funct...
Async await usage Usage: Put the keyword async in front of the function declaration to declare it is an asynchronous function What is the use of this async function? The next step is to look at await'...
Async belongs to the content of ES7. It defines an asynchronous function. This function returns a promise object, which can be called in the form of .then. eg: Await is waiting for the asynchronous ex...
Usingasync/awaitWhen I encountered an error: The error code is as follows: And the result of returning the test function is apromiseObject instead of the expected string or error object. Later, I quer...
promise and async are doing asynchronous processing, into the asynchronous synchronization 1.promise It is the purpose of Promise and birth are made to address a "callback hell" promise to u...
The emergence of promise In order to solve the js asynchronous problem, the results can be output in the form of a queue, making the code more readable new Promise((reslove,reject)=>{ // The return...