Skip to content

Instantly share code, notes, and snippets.

@nlitwin
Last active September 11, 2019 19:43
Show Gist options
  • Save nlitwin/9ed0a6c110071d26a66a07f772b7926d to your computer and use it in GitHub Desktop.
Save nlitwin/9ed0a6c110071d26a66a07f772b7926d to your computer and use it in GitHub Desktop.
Implementing async / await with generators
function doWhenDataReceived(value) {
returnNextElement.next(value)
}
function* createFlow() {
const data = yield fetch('http://twitter.com/...')
console.log(data)
}
const returnNextElement = createFlow() // Does not start executing function yet
const futureData = returnNextElement.next() // starts executing inside createFlow
futureData.value.then(doWhenDataReceived)
// With async
async function createFlow() {
const data = await fetch('https://twitter.com/...')
console.log(data)
}
createFlow()
@tapin13
Copy link

tapin13 commented Mar 11, 2019

Uncaught TypeError: futureData.then is not a function at ...

@darronz
Copy link

darronz commented Jul 7, 2019

function doWhenDataReceived(value) {
  returnNextElement.next(value);
}

function* createFlow() {
  const data =const data = yield fetch('http://twitter.com/...');
  console.log(data);
}

const returnNextElement = createFlow();
const futureData = returnNextElement.next();

// the promise is on the `value` key
futureData.value.then(doWhenDataReceived);

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