Skip to main content
added 5 characters in body
Source Link
T.J. Crowder
  • 1.1m
  • 201
  • 2k
  • 2k

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).

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

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);
    });

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).

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

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);
    });

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);
    });
added 1930 characters in body
Source Link
T.J. Crowder
  • 1.1m
  • 201
  • 2k
  • 2k

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).

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

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);
    });

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

No, there isn't. If you want to add a property to the promise being returned, you'll have to use a non-async function.

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).

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

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);
    });
Source Link
T.J. Crowder
  • 1.1m
  • 201
  • 2k
  • 2k

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

No, there isn't. If you want to add a property to the promise being returned, you'll have to use a non-async function.