0

I want to set the value of the result to the promise variable, which is JSON file.

I tried returns, creating the variable like (result) => {var myVar = result}

async function getUpdate() {
  async function getFirstEvent() {
    //code returning a JSON
  }
  async function getSecondEvent() {
    //code returning a JSON
  }

  let firstPull = await getFirstEvent(); //unimportant
  var dataPull1 = await JSON.stringify(firstPull); //unimportant

  var promise = new Promise(async function(resolve, error) {
    let secondPull = await getSecondEvent();
    var dataPull2 = await JSON.stringify(secondPull);

    setTimeout(() => resolve(dataPull2), 3000);
  }).then((result) => {
    //return a value to promise ???
  });
}

getUpdate();
setInterval(getUpdate, 5000);

I've received undefined, but that's because of the variable didn't get any value assigned to.

adiga
  • 34,372
  • 9
  • 61
  • 83
Zay Yadi
  • 3
  • 2
  • [Never pass an `async function` as the executor to `new Promise`](https://stackoverflow.com/q/43036229/1048572)! – Bergi Jul 30 '19 at 13:01
  • Where exactly did you try to get a `result` value but failed to do? – Bergi Jul 30 '19 at 13:02

3 Answers3

0

In future try to not mix async/await with Promise

function sleep(ms) {
 return new Promise((resolve) => setTimeout(resolve, ms));
}

async function getUpdate() {
 async function getFirstEvent() {
  //code returning a JSON
 }

 async function getSecondEvent() {
  //code returning a JSON
  return "result";
 }

 let firstPull = await getFirstEvent(); //unimportant
 var dataPull1 = await JSON.stringify(firstPull); //unimportant
 let secondPull = await getSecondEvent();
 var dataPull2 = await JSON.stringify(secondPull);
 await sleep(3000);
 console.log(dataPull2);
 return dataPull2;
}

getUpdate();
setInterval(getUpdate, 5000);
ponury-kostek
  • 7,824
  • 4
  • 23
  • 31
  • `In future try to not mix async/await with Promise` could you clarify this a bit? – Clarity Jul 30 '19 at 12:39
  • @Clarity mixing `async/await` with `Promise`, makes it harder to read and understand what is actually happening, also error handling could be more complicated – ponury-kostek Jul 30 '19 at 12:51
0

You should not use async / await here, it makes writing a solution much harder, rather, you should use .then() to consume a promise. You can return the data from the function, and use that data with .then():

const getUpdate = () => {
  const getSecondEvent = () => new Promise((resolve, reject) => resolve([1,2,3]))
  return getSecondEvent().then(JSON.stringify)
}

getUpdate().then(console.log)
Kobe
  • 6,226
  • 1
  • 14
  • 35
-1

Why would you create a promise at the "promise" variable if you actually want a value? Using your code, something like this should work:

var promise = await new Promise(async function(resolve, error) {
... // your function
resolve(data);
}

but that looks kinda nasty. For a better, cleaner code i'd take out the whole promise to an outside function which returns a promise, and then put var promise = await promiseFunc(), which looks much better.

Gibor
  • 1,695
  • 6
  • 20