Last active
January 9, 2020 04:20
-
-
Save wwwins/41e37774edc937e37649c030d8a6a6bc to your computer and use it in GitHub Desktop.
Promise and aysnc/await demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Promise and async/await function | |
*/ | |
'use strict'; | |
// type-1 | |
// function(resolve) {} | |
function action1(msg) { | |
return new Promise( | |
function(resolve) { | |
setTimeout(function() { | |
resolve(msg); | |
console.log('action1:'+msg); | |
}, 1000); | |
} | |
) | |
} | |
// type-2 | |
// resolve => {} | |
function action2(msg) { | |
return new Promise(resolve => { | |
setTimeout(() => { | |
resolve(msg); | |
console.log('action2:'+msg) | |
}, 1000); | |
}); | |
} | |
function action3(msg) { | |
return new Promise(resolve => { | |
setTimeout(() => { | |
resolve(msg); | |
console.log('action3:'+msg) | |
}, 1000); | |
}); | |
} | |
async function action4() { | |
let r1 = await action1('a1'); | |
let r2 = await action2('a2'); | |
return (r1+':'+r2); | |
} | |
// action4 vs action5 | |
async function action5() { | |
let r1 = action1('a1'); | |
let r2 = action2('a2'); | |
await r1; | |
await r2; | |
return r2; | |
//return {r1, r2}; | |
} | |
function getUserData() { | |
return new Promise(resolve => { | |
let user = { | |
name: "jacky", | |
mobile: "0933010234", | |
email: "[email protected]" | |
} | |
setTimeout(() => { | |
resolve(user); | |
console.log('getUesrData'); | |
}, 1000); | |
}) | |
} | |
function main1() { | |
console.log('main1'); | |
action1('a1') | |
.then(() => action2('a2')) | |
.then(() => action3('a3')) | |
} | |
function main2() { | |
console.log('main2'); | |
action1('pass from a1') | |
.then((msg) => action2(msg)) | |
.then((msg) => action3(msg)) | |
} | |
async function main3() { | |
console.log('main3'); | |
await action1('a1'); | |
await action2('a2'); | |
await action3('a3'); | |
} | |
async function main4() { | |
console.log('main4'); | |
let msg = await action1('pass from a1'); | |
await action2(msg); | |
await action3(msg); | |
} | |
async function main5() { | |
await action1('a1'); | |
let user = await getUserData(); | |
await action2(user.email); | |
console.log('done5'); | |
} | |
async function main6() { | |
let r = await action4(); | |
console.log('main6:'+r); | |
console.log('done'); | |
} | |
async function main7() { | |
let r2 = await action5(); | |
console.log('r2:'+r2); | |
console.log('done'); | |
} | |
async function main8() { | |
for (let i = 0; i<10; i++) { | |
await action1('a1'); | |
} | |
console.log('done'); | |
} | |
async function main9() { | |
let arr = []; | |
for (let i=0; i<10; i++) { | |
arr.push(action1('a'+i)); | |
} | |
console.log('parallel tasks') | |
for (let j=0; j<10; j++) { | |
await arr[j]; | |
} | |
console.log('done'); | |
} | |
console.log('start'); | |
//main1(); | |
//main2(); | |
//main3(); | |
//main4(); | |
//main5(); | |
//main6(); | |
//main7(); | |
//main8(); | |
main9(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment