3 ms·
Exactly. The error object is a return value of the "await"-ed function, not an exception that's thrown. Here's a concrete example. Normally in Node you do some
by tlrobinson 15y ago
Exactly. The error object is a return value of the "await"-ed function, not an exception that's thrown.
Here's a concrete example. Normally in Node you do something like this:
fs.readFile('/etc/passwd', function (err, data) {
if (err) {
// handle error
}
// normal processing
});
The await version would look like this:
await err, data = fs.readFile('/etc/passwd');
if (err) {
// handle error
}
// normal processing
Ideally it would look like this:
try {
await data = fs.readFile('/etc/passwd');
// normal processing
} catch (error) {
// handle error
}
- koush 15y agoThis is certainly something that is doable and crossed my mind, but I did not want to have the await be restricted to functions that conform to the typical return arguments. For now at least.
- dbrock 15y agoHow about try await foo = bar(baz) as shorthand for await error, foo = bar(baz) if (error) throw error