0

In the code below , Values are RETURNED correctly from a queued Promise.then() chain .

CODE:

let cond_1 = true;
let data = 'Data Received....';
let err  = 'Error';

var p1 = new Promise(function(resolve,reject){ 
    if(cond_1){
        resolve(data);
    }else{
        reject(err); }})

p1.then((data)=>{console.log(data);return 'Wait....';})
.then((val1)=>{console.log(val1); return 'Finished';})
.then((val2)=>{console.log(val2)})
.catch((err)=>{console.log(err)});

Output :

Data Received....
Wait....
Finished

However, the same RETURNED values from a chained SetTimeout function are returned 'UNDEFINED'.

CODE:

p1.then((data)=>{console.log(data); return 'Wait.....'; })
.then((val1)=>{setTimeout(function(val1){console.log(val1); return 'Finished';},1000)})
.then((val2)=>{setTimeout(function(val2){console.log(val2);},1000)})
.catch((err)=>{console.log(err)});

Output:

Data Received....
undefined
undefined

How to resolve this?

srt111
  • 1,079
  • 11
  • 20
  • 2
    `setTimeout` does not return a promise. Just a timer ID. And you don't return that one either, in your code. "Solving" this depends on what you actually want to do. You could [make a delay](https://stackoverflow.com/a/39914235) or even modify it slightly to produce a value after some time. But I'm not really sure what the intention is here. Why would you need to use `setTimeout`? – VLAZ Mar 19 '22 at 10:38
  • @VLAZ ... The above script is just a short test for queued execution with PRE_DEFINED DELAY and the code in the function block would like to replaced a larger codeblock. However the code EXECUTES AT ONCE (SetTimeout@1000ms) instead of each block executing after 1000ms ONE AFTER ANOTHER. ..... Are you saying that a new PROMISE has to be created for each .Then() ? – srt111 Mar 19 '22 at 12:31

1 Answers1

1

Try taking advantage of Lexicographic nature of Javascript.

Instead of making a function v1,v2 which your functions takes within setTimeout, just use an arrow function. In this way you are using the v1,v2 returned from promise.

Do this

let cond_1 = true;
let data = 'Data Received....';
let err = 'Error';

var p1 = new Promise(function(resolve, reject) {
  if (cond_1) {
    resolve(data);
  } else {
    reject(err);
  }
})

p1.then((data) => {
    console.log(data);
    return 'Wait.....';
  })
  .then((val1) => {
    setTimeout(() => {
      console.log(val1);
    }, 1000);
    return 'Finished';
  })
  .then((val2) => {
    return setTimeout(() => {
      console.log(val2)
    }, 1000)
  })
  .catch((err) => {
    console.log(err)
  });

What you did was you created a new variable v1,v2 for your function. You can only use that when you pass value v1,v2 in that function. That function won't use v1,v2 returned from promise as you expect.

  • "*Lexicographic nature of Javascript.*" do you mean [lexical](https://en.wikipedia.org/wiki/Scope_(computer_science)#Lexical_scope) instead of [lexicographic](https://en.wikipedia.org/wiki/Lexicographic_order)? – VLAZ Mar 19 '22 at 11:00
  • @Shivansh Verma ....The code EXECUTES AT ONCE (SetTimeout@1000ms) instead of each block executing after 1000ms - 'ONE AFTER ANOTHER'. – srt111 Mar 19 '22 at 12:32
  • @VLAZ ,@Shivansh Verma ...Any way to chain the SetTimeout with a delay of 1000ms 'EACH' (rather than each SetTimeout@1000ms getting executed at once). – srt111 Mar 19 '22 at 13:02
  • @VLAZ yes I mean lexical nature. You are right. – Shivansh Verma Mar 21 '22 at 06:53
  • @starzar If you mean to execute a block after some definite interval again n again lets say 1000ms then you should use setInterval instead of setTimeout – Shivansh Verma Mar 21 '22 at 06:55