await operator should not be used on a non-Promise value

  • AWAIT_NON_PROMISE
  • Code Quality
  • Low
  • es8

This rule applies when the await operator is used on a non-Promise value.

await operator pauses the execution of the current async function until the operand Promise is resolved.
When the Promise is resolved, the execution is resumed and the resolved value is used as the result of the await.

Although awaiting a non-Promise value is possible under the JavaScript specification (it will be converted to a resolved Promise), it is often a programmer's mistake and may cause an unexpected order of execution.

Noncompliant Code Example

View with compliant examples side by side
function say(msg) {
    return (() => {
        console.log(msg);
        return 'Done';
    });
}
function doSomethingAfter1Sec(something) {
    setTimeout(() => {
        something();
    }, 1000);
}
async function asyncCall() {
    console.log('Start');
    var result = await doSomethingAfter1Sec(say('Hi')); // AWAIT_NON_PROMISE alarm because 'doSomethingAfter1Sec(...)' is not Promise.
    console.log('Result: ' + result);
    console.log('End');

    console.log('Start');
    await [1, 2, 3].map(x => doSomethingAfter1Sec(say(x))); // AWAIT_NON_PROMISE alarm because awaiting an array has no effect.
    console.log('End');
}

Compliant Code Example

View with noncompliant examples side by side
function say(msg) {
    return (() => {
        console.log(msg);
        return 'Done';
    });
}
function doSomethingAfter1Sec(something) {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve(something());
        }, 1000);
    });
}
async function asyncCall() {
    console.log('Start');
    var result = await doSomethingAfter1Sec(say('Hi'));
    console.log('Result: ' + result);
    console.log('End');

    console.log('Start');
    await Promise.all([1, 2, 3].map(x => doSomethingAfter1Sec(say(x))));
    console.log('End');
}

Version

This rule was introduced in DeepScan 1.24.0.

See

Was this documentation helpful?