asyc try-catch

u.go
2016-02-06T07:21:29Z

asyc.md

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

return-async.md

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