Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // resolves second
- async function a(context) {
- const delay = 200;
- return new Promise((resolve) => {
- setTimeout(() => {
- console.log(context + ' ' + delay);
- resolve();
- }, delay);
- });
- }
- // resolves third
- async function b(context) {
- const delay = 500;
- return new Promise((resolve) => {
- setTimeout(() => {
- console.log(context + ' ' + delay);
- resolve();
- }, delay);
- });
- }
- // resolves first
- async function c(context) {
- const delay = 100;
- return new Promise((resolve) => {
- setTimeout(() => {
- console.log(context + ' ' + delay);
- resolve();
- }, delay);
- });
- }
- async function awaitEx() {
- try {
- // these will print in the same order 1-2-3
- await a("awaiting 1");
- await b("awaiting 2");
- await c("awaiting 3");
- } catch (ignored) {
- }
- }
- async function noAwait() {
- try {
- // these will print in the order the promises resolve
- a("not awaiting 1");
- b("not awaiting 2");
- c("not awaiting 3");
- } catch (ignored) {
- }
- }
- noAwait();
- awaitEx();
Add Comment
Please, Sign In to add comment