Would you like to clone this notebook?

When you clone a notebook you are able to make changes without affecting the original notebook.

Cancel

async/await

node v0.12.18
version: 1.0.0
endpointsharetweet
A common way to get around nesting callbacks everywhere in JavaScript is to use promises. But these require chaining a bunch of "then" functions together to do anything, which is still cumbersome. Putting "await" before any promise let's you write linear looking code, which works great with Tonic and our object viewers that show you the value of the last expression:
// the old way require('request-promise')("https://status.github.com/api/status.json") .then(function(status){ // do something with status }) // the new way! await require('request-promise')("https://status.github.com/api/status.json")
Many libraries these days natively support promises, and npm is full of packages that add promise support to existing libraries. Here are a few of our favorites:
require("fs-promise") // promise based filesystem api
require("request-promise") // a wrapper around "request" for http stuff
require("glob-promise") // glob style filesystem queries
require("bluebird") // general promise library with lots of utilities
Loading…

no comments

    sign in to comment