Skip to content

Instantly share code, notes, and snippets.

@maowug
Last active March 21, 2016 10:10
Show Gist options
  • Save maowug/ae46566975a2d60c36fb to your computer and use it in GitHub Desktop.
Save maowug/ae46566975a2d60c36fb to your computer and use it in GitHub Desktop.
asyc try-catch
function a() {
  return new Promise((resolve, reject)=> {
    setTimeout(function() { reject('//a') }, 100);
  })
}

async function b() {
  console.log("await "+ await a())
  throw '//b '
  console.log("return")
  return await a()
}

b().catch(function(val) { console.log(val) })
//OUTPUT:
//a

throw b before await a

function a() {
  return new Promise((resolve, reject)=> {
    setTimeout(function() { reject('//a') }, 100);
  })
}

async function b() {
-  console.log("await "+ await a())
  throw '//b '
  console.log("return")
  return await a()
}

b().catch(function(val) { console.log(val) })
//OUTPUT:
-//a
+//b

try only catch when await b()

// b().catch(function(val) { console.log(val) })

async function c() {
  try {
+    await b() // --- (1)
  } catch(e) {  // --- (2)
    throw e + '//[re c]'
  }
}

c().catch(function(val) { console.log(val) })
//OUTPUT:
//b //[re c]

async throws native throws imediatyly

async function c() {
+  throw "//c"
  try {
    await b() // --- (1)
  } catch(e) {  // --- (2)
    throw e + '//[re c]'
  }
}

c().catch(function(val) { console.log(val) })
//OUTPUT:
-//b //[re c]
+//c
function a() {
  return new Promise((resolve, reject)=> {
    setTimeout(function() { resolve('a') }, 100);
  })
}

async function b() {
  // console.log("await "+ await a())
  // throw '//b '
  return a()
}

b()
.then((v)=> console.log(v))
.catch(function(val) { console.log(val) })

compiled to

'use strict';

function a() {
  return new Promise(function (resolve, reject) {
    setTimeout(function () {
      resolve('a');
    }, 100);
  });
}

function b() {
  return regeneratorRuntime.async(function b$(context$1$0) {
    while (1) switch (context$1$0.prev = context$1$0.next) {
      case 0:
        context$1$0.next = 2;
        return regeneratorRuntime.awrap(a());

      case 2:
        return context$1$0.abrupt('return', context$1$0.sent);

      case 3:
      case 'end':
        return context$1$0.stop();
    }
  }, null, this);
}

b().then(function (v) {
  return console.log(v);
})['catch'](function (val) {
  console.log(val);
});

// console.log("await "+ await a())
// throw '//b '

when return await a(), update ↑, not very clear

   return regeneratorRuntime.async(function b$(context$1$0) {
     while (1) switch (context$1$0.prev = context$1$0.next) {
       case 0:
-        return context$1$0.abrupt('return', a());
+        context$1$0.next = 2;
+        return regeneratorRuntime.awrap(a());
-      case 1:
+      case 2:
+        return context$1$0.abrupt('return', context$1$0.sent);
+
+      case 3:
       case 'end':
         return context$1$0.stop();
     }

return await or just return async?

same result, but when return await a() does it mean wrapping the await result in new Promise? the other way, if return an async one(Promise) directly, the caller async function just return it untouched?

function a() {
  return new Promise((resolve, reject)=> {
    setTimeout(function() { reject('//a') }, 100);
  })
}
 async function b() {
  // console.log("await "+ await a())
  // throw '//b '
  try {
-    return a()
+    return await a()
  } catch(e) {
    throw '//re b'
  }
}

b()
.then((v)=> console.log(v))
.catch(function(val) { console.log(val) })
//OUTPUT:
-////a
+////re b
@maowug
Copy link
Author

maowug commented Mar 21, 2016

better have some new Promise({...})

uncautght

Promise.resolve((function(){throw new Error("throwed")})()).then(function(value){
    console.log(value);
  throw new Error("throwed in then")
}).catch(function(e){
  console.log('catched:', e.toString())
})

uncautght

new Promise((function(){throw new Error("throwed3")})()).then(function(value){
    console.log(value);
  throw new Error("throwed in then")
}).catch(function(e){
  console.log('catched:', e.toString())
})

caught

new Promise( _ => {throw new Error("throwed2")}).then(function(value){
    console.log(value);
  throw new Error("throwed in then")
}).catch(function(e){
  console.log('catched:', e.toString())
})

_but_ new Promise( _ => 1).then(function(value){ unexpected

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment