Adidi
Adidi

Reputation: 5253

Access promise instance that returns from async function

Async function automatically returns promise - I wonder if there is a way somehow to get this instance of this promise inside the function For example if I return an actual promise like this:

const getSomePromise = () => {
    const promise = new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve('success');
       }, 1000);
    })

    promise.someProp = 'myProp';
    return promise;
}

const promise = getSomePromise();
console.log(promise.someProp);

I want to achieve the same thing with pure async function:

const sleep = ts => new Promise(resolve => setTimeout(resolve, ts));

const getSomePromise =  async () => {
     const p = await sleep(1000);
     // some how access the instance of the promise from within the async function 
     // for example this['someProp'] = 'myProp';
     // and return the all promise with this prop
     return 'sucess';
}
const promise = getSomePromise();
console.log(promise.someProp);

Can I do that ?

Thanks

Upvotes: 1

Views: 571

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1073968

Adding a property to the promise is almost certainly a bad idea (more on that later, under However), but just to talk about how you would continue to do it:

I wonder if there is a way somehow to get this instance of this promise inside the function

No, there isn't. You could create a promise within the function and return it, but that wouldn't be the promise the function returns (it would just affect how the promise the function returns resolves).

If you want to add a property to the promise being returned, you'll have to use a non-async function. You might make the function's entire code non-async:

const sleep = ts => new Promise(resolve => setTimeout(resolve, ts));

const getSomePromise = () => {
    const p = sleep(1000).then(() => 'success');
    p.someProp = 'myProp';
    return p;
}
const promise = getSomePromise();
console.log(promise.someProp);

...or you might use an inner async function so you can use await semantics and such:

const sleep = ts => new Promise(resolve => setTimeout(resolve, ts));

const getSomePromise = () => {
    const p = (async () => {
        await sleep(1000);
        return 'success';
    })();
    p.someProp = 'myProp';
    return p;
}
const promise = getSomePromise();
console.log(promise.someProp);

However: Adding a property to the promise is almost certainly a bad idea. Instead, have the promise resolve to an object with properties both for the resolution and the extra someProp:

const sleep = ts => new Promise(resolve => setTimeout(resolve, ts));

const getSomePromise = async () => {
    const p = await sleep(1000);
    // some how access the instance of the promise from within the async function 
    // for example this['someProp'] = 'myProp';
    // and return the all promise with this prop
    return {
        result: 'success',
        someProp: 'myProp'
    };
}
getSomePromise()
    .then(resolution => {
        console.log(resolution.someProp);
    });

Upvotes: 2

Related Questions