0

I understand to access a value of a Promise, I will use the "then" callback attach to it.

However, I was wondering how I can affect the context outside of it?

let outsideVar = 0;
promiseA.then(value => {
    outsideVar = 1;
})
console.log(outsideVar); //0

As you can see I would like to change outsideVar to 1

[Edit] This question was to see if promises can affect global variables outside of it and if there were any possibilities to do that.

Unfortunately, it is not possible.

Anraiki
  • 786
  • 1
  • 10
  • 26
  • use `promiseA.then(value => { outsideVar = 1; }).then(()=>{ console.log(outsideVar);});` The `console.log` is not waiting for the promise to finish. – Sayan Pal Jun 05 '17 at 17:32
  • 2
    Short answer: you can't. – Jared Smith Jun 05 '17 at 17:35
  • @JaredSmith What about using a closure? – jdmdevdotnet Jun 05 '17 at 17:39
  • 2
    @jdmdevdotnet no. Closures deal with scope. The problem here is *time*. You can't eat the BigMac before you've even made it to McDonalds, no matter how fancy your method of transportation (unless it's a Tardis). – Jared Smith Jun 05 '17 at 17:51
  • Very true, ok, thanks. I thought there was a way to manipulate outside objects via closure, but you're right it might change the value but not in the time needed. – jdmdevdotnet Jun 05 '17 at 18:16

2 Answers2

5

Your problem isn't changing your outsideVar, it's that you're checking it way too soon.

The Promise is async, so you need to wait until it is complete to output.

let outsideVar = 0;
promiseA.then(value => {
     outsideVar = 1;
}).then(() => console.log(outsideVar));

Once you start using a Promise, you need to keep writing async code.

You could use await and async, if you're using a preprocessor like Babel with es2017 presets.

There are some caveats with them though:

  • You can't run it directly at the top-level, it'd have to be in a function marked async.
  • The code is still running asynchronously, it just looks synchronous. Be aware of this difference or weird things will happen.

Here is the example:

let outsideVar = 0;    
const doSomething = async () => {
    await Promise.resolve(); // something that is async
    outsideVar = 1;
};

await doSomething();
console.log(outsideVar);
samanime
  • 25,408
  • 15
  • 90
  • 139
  • Darn. I thought it was possible. Should I investigate Async/Await to handle this? – Anraiki Jun 05 '17 at 17:38
  • `async` and `await` will give you some syntactical sugar to help with this, yes. I'll update my answer with an example of using them. – samanime Jun 05 '17 at 17:44
  • 1
    `await doSomething();` is invalid. You can only use `await` inside an `async` function (I know you are saying this in the text, but the example is still invalid). – Felix Kling Jun 05 '17 at 18:02
  • Seems like the outsideVar enters the "doSomething" function context and does not affect the "global" outsideVar. – Anraiki Jun 05 '17 at 18:02
  • @Anraiki no, it *does* affect it, it is just not guaranteed to do so at any particular *time*, in this case the time your `console.log` runs. You are not alone in struggling to grasp this concept: it's hard for a lot of people, but until you do you are not going to be able to wallpaper over the difficulty with syntax. We talk about 'synchronous' vs 'async', but a better way to say it might be code that does not run *sequentially* from top to bottom. The `console.log` runs before the mutation, but the mutation still happens. Eventually. – Jared Smith Jun 05 '17 at 18:04
  • @FelixKling Indeed, but it makes the example too clunky to show too many levels of functions. I wanted to boil it down to just one single example. – samanime Jun 08 '17 at 13:54
  • Given that there many people who have problems understand asynchronous behavior in JavaScript or how `async/await` works, I think it could be really misleading. Why not just write `doSomething().then(() => console.log(outsideVar));`? – Felix Kling Jun 08 '17 at 13:59
-1

`

let ousideVar = 0;

function promiseA(){
 return new Promise( function(resolve, reject){
  resolve();
 })
}

promiseA().then(function(){
 ousideVar = 1;
  //when promise resolved
  console.log(ousideVar) // 1  
})

console.log(ousideVar) // 0

`When your promise resolved your "outsideVar" replace with the new value inside of then.
Example:

let outsideVar = 0;
promiseA().then(value => {
outsideVar = 1;
})
console.log(outsideVar);

When promiseA calls it return a callback and your value replaced with the new one
outsideVar = 1;