Skip to content

Instantly share code, notes, and snippets.

@jbaylina jbaylina/proposal
Last active Aug 29, 2015

Embed
What would you like to do?
ecma7 proposat change async/await by async/nowait
The main idea is to maintain all the logic of the async/await mechanism except that all calls to async functions are "await implicit". And if you want to get the promise of an async function call it with a nowait in front.
Example of code:
Actual way:
var myObj = {
getProp1: async function() {
if (this.prop1) return prop1;
return new Promise(function(resolve) {
setTimeout(function() {
resolve(10)
}, 5000)
});
},
getProp1: async function() {
if (this.prop1) return prop1;
return new Promise(function(resolve) {
setTimeout(function() {
resolve(5)
}, 5000)
});
}
}
async function calculate() {
reult = (await myObj.getProp1() - await myObj.getPro2) / ( await myObj.getProp1() + await myObj.getPro2);
console.log(result);
}
calculate();
Proposed way:
var myObj = {
getProp1: async function() {
if (this.prop1) return prop1;
return new Promise(function(resolve) {
setTimeout(function() {
resolve(10)
}, 5000)
});
},
getProp1: async function() {
if (this.prop1) return prop1;
return new Promise(function(resolve) {
setTimeout(function() {
resolve(5)
}, 5000)
});
}
}
async function calculate() {
reult = (myObj.getProp1() - myObj.getPro2) / ( myObj.getProp1() + myObj.getPro2);
console.log(result);
}
nowait calculate();
The rules and logic is exactly the same that async awati.
An async function can be called directly only from an async fnction.
nowait of an async function returns the promise.
nowait of a regular function returns a resolved promise.
await of a promise can only be called from an async function.
@bergus

This comment has been minimized.

Copy link

commented Mar 28, 2015

Are you seriously suggesting that every (every!) single reference to a promise, where it should not wait, needs to be marked with nowait - instead of simple marking those places where you want to wait with await?

// ES7:
let result = await Promise.all([getProp1(), getProp2()]).then([p1, p2] => (p1 - p2) / (p1 + p2));
// your proposal:
let result = Promise.all([nowait getProp1(), nowait getProp2()]).then([p1, p2] => (p1 - p2) / (p1 + p2));
@bergus

This comment has been minimized.

Copy link

commented Mar 28, 2015

An async function can be called directly only from an async fnction.

Or else what? Notice that the current proposal does not distinguish between async functions and normal function - both may just return a promise when being called. Regardless from where they are called.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.