node package manager

seem

Promises and Generators

Promises and Generators

This module allows you to implement ES7's async/await pattern using ES6's yield.

This is @ForbesLindesay's async function from his Control Flow Utopia presentation. I was surprised to find out it apparently hadn't been packaged yet.

var seem = require('seem');
 
var g = seem(function*(n){
  var a = yield Promise.resolve(n+4);
  var b = yield Promise.resolve(a+5);
  return b+3;
});

The function will now return a Promise that will resolve to the value of the return statement upon success.

g(2).then(function(result){ assert(result === 2+4+5+3); })

Essentially yield turns its Promise argument into that Promise's result.

Replace Promise.resolve with your preferred asynchronous operation for full power.