Last active
July 25, 2018 05:11
-
-
Save Misaka-0x447f/cd544d31b4572e75dc2e6ff0883b17fa to your computer and use it in GitHub Desktop.
async/await getting started in light speed
This file contains 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
<script> | |
function succeedTarget(tip = false) { | |
let promise = new Promise((resolve, reject) => { | |
setTimeout(() => { | |
resolve({ | |
result: "success", | |
data: "oo" | |
}); | |
tip ? console.log("// resolve or reject won't exit your code") : {}; | |
}, 500) | |
}); | |
promise.then(function () { | |
tip ? console.log("// you can also mount some code here.") : {}; | |
}); | |
return promise; | |
} | |
function failedTarget() { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
reject({ | |
result: "error", | |
error: "xx" | |
}); | |
}, 500) | |
}) | |
} | |
async function blockingCall1() { | |
console.log("blocking call 1 ---------------"); | |
// method 1: using .then / .catch to process your promise. | |
await succeedTarget(true) | |
.then(function(data){ | |
console.log(data); | |
}) | |
.catch(function(err){ | |
console.log(err); | |
}) | |
// method 2: using try / catch to process your promise (grouply process exception?). resolving as a expression. | |
try { | |
console.log(await failedTarget()); | |
} catch (err) { | |
console.log(err); | |
} | |
} | |
async function blockingCall2() { | |
console.log("blocking call 2 ---------------"); | |
// if we are not catching exceptions. | |
console.log(await succeedTarget()); | |
console.log(await failedTarget()); | |
} | |
function nonBlockingCall() { | |
// without "async", we can parallely resolve promise. | |
// if you would like to resolve your promise grouply, you can do it like the second file. | |
succeedTarget().then(function(response){ | |
console.log("non-blocking async done with " + response["result"]); | |
}) | |
console.log("non-blocking async call ------"); | |
} | |
function run() { | |
blockingCall1(); | |
console.log("// async function will be run in another thread"); | |
setTimeout(blockingCall2, 5000); | |
setTimeout(nonBlockingCall, 10000); | |
setTimeout(() => {console.log("// End of Program")}, 15000); | |
} | |
run(); | |
</script> |
This file contains 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
<script> | |
function promise(x, will_resolved = true) { | |
return new Promise((resolve, reject) => { | |
console.log(`${x} start`); | |
setTimeout(() => { | |
will_resolved === true ? resolve(x) : reject(x); | |
}, 1000) | |
}) | |
} | |
async function run() { | |
setTimeout(async() => { | |
console.log("Promise.all is not an utility designed for isolate promises."); | |
Promise.all([ | |
await promise("A1"), | |
promise("A2") | |
]) | |
Promise.all([ | |
promise("B1") | |
]) | |
}, 10); | |
setTimeout(async() => { | |
console.log("You will need a function expression to concurrent run your promises."); | |
(async() => { | |
await promise("A1"); | |
promise("A2"); | |
})(); | |
promise("B1"); | |
}, 5000); | |
setTimeout(async() => { | |
console.log("And, promises without await won't be catched by try..catch"); | |
try { | |
await promise("A1", false); | |
promise("A2", false); | |
promise("A3", false); | |
} catch (e) { | |
console.log(`caughted error: ${e}`); | |
} | |
}, 10000); | |
setTimeout(async() => { | |
console.log("-------------------"); | |
try { | |
promise("A1", false); | |
promise("A2", false); | |
promise("A3", false); | |
} catch (e) { | |
console.log(`caughted error: ${e}`); | |
} | |
}, 14000); | |
setTimeout(() => { | |
console.log("%cThis is an intended error.", "color: rgba(0,0,0,0.4)"); | |
}, 16000); | |
setTimeout(async() => { | |
console.log("So you have to catch something unexcepted by Promise.all.catch"); | |
Promise.all([ | |
promise("A1", false), | |
promise("A2") | |
]).catch(error => { | |
console.log(`caughted error: ${error}`); | |
}); | |
}, 20000); | |
setTimeout(() => { | |
console.log("%c--- EOF ---", "background: #6cf"); | |
}, 23000) | |
} | |
run(); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment