Skip to content
This repository was archived by the owner on Jan 26, 2022. It is now read-only.
This repository was archived by the owner on Jan 26, 2022. It is now read-only.

Clarification of execution of next() and throw() with await  #93

@gskachkov

Description

@gskachkov

During testing async iteration I faced with unexpected result:

const getPromise = ph => {
    return new Promise((resolve, reject) => {
        ph.resolve = resolve;
        ph.reject = reject;
    });
};
const promiseHolder = {};
const promise = getPromise(promiseHolder);

async function *foo() {
    yield '#1';
    var result = await promise;
    yield result + '#2';
    yield result + '#3';
};

const f = foo();

f.next().then(({ value })=> print('fulfilled #1', value));
f.next().then(({ value })=> print('fulfilled #2', value));

promiseHolder.resolve('success');

f.throw(new Error('Some error')).then(({ value })=> print('fulfilled #3', value), error=> print('reject #3', error));
f.next().then(({ value })=> print('fulfilled #4', value));

I expect following print:

fulfilled #1 #1
fulfilled #2 success#2
reject #3 Error: Some error
fulfilled #4 undefined
// But I received 
fulfilled #1 #1
reject #3 Error: Some error
fulfilled #2 success#2
fulfilled #4 undefined

As I can understand this happened because we do not directly resolve promiseCapabilty but use valueWrapperCapability:
6.4.3.3 AsyncGeneratorResolve
....
6.4.3.3.10. Perform ! PerformPromiseThen(valueWrapperCapability.[[Promise]], onFulfilled, undefined, promiseCapability)
6.4.3.3.11.Perform ! AsyncGeneratorResumeNext(generator).
and in case of throw() we print result earlier because in 6.4.3.4 AsyncGeneratorReject we reject promiseCapabilty without any wrapper.

Could you please suggest is this something wrong in my implementation or it works as expected?

Best regards,
Oleksandr

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions