Rahul
Rahul

Reputation: 21

Unable to resolve a Promise calling another method

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

Upvotes: 1

Views: 212

Answers (2)

marcdahan
marcdahan

Reputation: 3082

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

Upvotes: 0

Mulan
Mulan

Reputation: 135377

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 -

Upvotes: 1

Related Questions