The improvement on the 8.0.0 version is that such an "oldie" function can be Promisified. A great improvement.
e.g. function myCallback (err, result) {...}[0]: https://medium.com/@styfle/promises-in-node-js-8-x-core-d6a8...
https://stackoverflow.com/questions/44260289/using-native-es...
The biggest additions here with regard to async/await is the new promisify util and the async_hooks.
Edit: Changed wording and included more detail.
Edit: originally wrote 'unstable'. Meant 'non-LTS'
I don't think that's required. Superagent already returns promises.
https://visionmedia.github.io/superagent/#promise-and-genera...
Alas too late to edit comment.
const util = require('util'),
fs = require('fs'),
writeFile = util.promisify(fs.writeFile),
request = require('superagent'),
stat = util.promisify(fs.stat),
sorts = require('sorts'),
log = console.log;
const getPhotosAndSaveThem = async function(){
try {
const response = await request.get('https://jsonplaceholder.typicode.com/photos');
const photos = response.body.sort(sorts.byKey('title'))
await writeFile('photos.json', JSON.stringify(photos, null, 2))
const status = await stat('photos.json');
log(`I got some data from an API and saved the responses at ${status.birthtime}`)
} catch (e) {
log(`Oh no, something went wrong!`, e)
}
}
getPhotosAndSaveThem() awaitAll
to replace await Promise.all
calls.Also, many times you want to limit the concurrency of your promise execution which isn't something you can do with an array of promises. You'd be back to using `await` + something like https://www.npmjs.com/package/promise.map.
I'm someone that used to use `co` where you could go:
const [a, b] = yield [promiseA(), promiseB()]
But I prefer the simplicity and consistency of having to use something like Promise.all or require('promise.map').You have a point with concurrency, but handling concurrency is an another beast altogether.
How does one get to the point where they want to await multiple promises, yet they need to be insulated from the very presence of the `Promise` object?
const [foo, bar, baz] = await returnerOfPromiseCollection();
That would assume there was either a) a catch in that collect or b) some error checking, but still...The code is awaiting an array, which is already "resolved" and its returned right away. Its contents are not relevant to `await`.
Yes, once you have a promise you have to deal with it as such through the whole chain.
I prefer something like p-settle most of the time https://github.com/sindresorhus/p-settle
const awaitAll = Promise.all.bind(Promise);
async function foo() {
var [val1, val2] = await awaitAll([func1, func1]);
}
It isn't ideal but it's a little cleaner const log = console.log.bind(console),
query = document.querySelector.bind(document),
queryAll = document.querySelectorAll.bind(document); await all([fn1, fn2]) ;)
i wonder when we will get s-expressions for function calls (awaitAll [fn1, fn2])https://github.com/nextorigin/riemann-query-parser/blob/mast...
const awaitAll = (futures) => futures.map(f => await f);
edit: this is wrong but I can't delete it. Haven't used async as much as promises.2) A request can fail and can timeout.
3) Writing to a file can fail.
And you get the idea. Don't omit error handling. Error handling is business logic too.
Being relaxed around error handling is the recipe for cascading server failure.
You should see the destructive effects of bad examples there.
You don't have enough information to be outraged at the toy example.
But in this case encapsulation is a leaky abstraction that just makes the diagnosing of an error more cumbersome.
You will eventually prove yourself wrong, since deemphasizing error handling goes wrong very quickly.
Then they added unhandled rejection warnings by default, and all was ok again - but I see why someone might insist that all examples have error handling.
.catch(error => {
process.nextTick(() => { throw error; });
})That means error handling may not be necessary inside that function. Errors are caught automatically and returned to the caller as rejected promise! You need error handling at the top level but not necessarily inside each function.