1

I am unable to resolve a Promise that is created. Not sure where the problem is, please help me to resolve this.

const tsearch = async () => {
    console.log("calling")
    //requesting the relevant page data
    await new Promise((resolve) =>  {
        getData("wifi", 2, true); 
        return resolve("success")
    });
    console.log(finished);
}

function getData(url, callControl = 0, wifi = false) {
    if (!!url) {
        console.log(url + " - " + callControl)
    }
    if (callControl > 0)
    setTimeout(getData, 1000, url, --callControl)
    else {
        console.log("getData - else part - resolving")
        // Promise.resolve();
    }
}


tsearch();
SuperStormer
  • 4,997
  • 5
  • 25
  • 35
Rahul
  • 21
  • 2
  • you're resolving immediately - the timeouts in getData don't prevent your call to resolve – Jaromanda X May 01 '21 at 00:47
  • You have to pass resolve into getData to so that getData can call it when it finishes. – Charlie Bamford May 01 '21 at 00:48
  • modified the getData call as `getData("wifi", 2, true, resolve);` and in getData() method added `Promise.resolve(resolve);` Still doesnot seem to work, am I missing something. – Rahul May 01 '21 at 02:27

2 Answers2

1

async and await is designed to make your programs easier to write. It's astounding how many people use it to make their programs more complicated. There are numerous misunderstandings presented in your question, and many others presented in other answers in this post -

async function tsearch () {
  console.log("calling")
  const result = await getData("wifi", 2, true)
  console.log("finished tsearch")
  return result
}

async function getData (url, callControl, wifi = false) {
  while (callControl > 0) {
    console.log(`${url} - ${callControl}`)
    callControl--
    await sleep(1000)
  }
  return "done"
}

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

tsearch().then(console.log, console.error)
calling
wifi - 2
wifi - 1
finished tsearch
done

If you want to write getData recursively, that is still an option, as is the case with any async function. This second example has the exact same behaviour and produces the same output -

async function tsearch () {
  console.log("calling")
  const result = await getData("wifi", 2, true)
  console.log("finished tsearch")
  return result
}

async function getData (url, callControl, wifi = false) {
  if (callControl <= 0) return "done"
  console.log(`${url} - ${callControl}`)
  await sleep(1000)
  return getData(url, callControl - 1, wifi)
}

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

tsearch().then(console.log, console.error)

For more info on misuse of async and await, please see these related Q&A's -

Mulan
  • 129,518
  • 31
  • 228
  • 259
0

I had to refactor the code

  1. removing the setTimeout which aren't necessary
  2. and adding a chaining of promises.

(() => {
function getData(url, callControl, boo, response) {
    if (response ) console.log(response)
    return new Promise(function(resolve, reject) {
        console.log("calling")
        let res = `: url: "${(url)}", callControl: "${(callControl)}" + boo: "${(boo)}"`
        if (!url || callControl <= 0) res = `NOT resolved + ${res}`
        else res = `RESOLVED + ${res}`
        resolve(res)
    });
}


let tsearch = new getData("wifi", 0, true)

tsearch
.then(result => {return new getData("wifi", 2, true, result)})
.then(result => {return new getData(null, 2, true, result)})
.then(result => console.log(result))
.catch(reason => console.log(reason))
.finally(() => console.log("Done"))
})();
marcdahan
  • 2,654
  • 25
  • 25
  • Why the code duplication, why two timeouts? I don't get how your refactoring is doing the same thing as the OP's code. – Bergi May 01 '21 at 11:19
  • Bergi exact, in a way it was pedagogic; but well it's ok I will reduce it ASAP – marcdahan May 01 '21 at 11:39