2

function one(){
    console.log(1);
}
function two(callback){
    setTimeout(()=>{
        console.log(2);
    },2000);
    callback();
}
two(one);

While I am running this code, 1 is displaying first and then 2 because 2 is taking 2 second time to display. Suppose if there is an api instead of console.log(2) in function two which is taking 2 second to respond, so How I can call function one after completion of function two using callback(); which I can do in this case if I use callback() inside setTimeout function but what if there is an api where I am not using setTimeout and it's taking 2-3 second?

3
  • You can call api with async - await, so that 2nd function will be called after api call is completed :) Commented Jan 31, 2020 at 10:57
  • I want to call 2nd function first which is taking longer time then function 1 using callback. Is that possible? Commented Jan 31, 2020 at 11:02
  • call your second function first with async and then call the first function on completing second. Simple. Bro think simple it will resolve all questions Commented Jan 31, 2020 at 11:05

4 Answers 4

4

Even if there is an api call which takes time to resolve, you can still use a callback at the same way you just did with the setTimeout example.

Alternatively, use a Promise:

function two(callback) {
    new Promise(res => {
        setTimeout(() => {
            res();
        }, 2000);
    }).then(callback)
}

or using async/await syntax:

async function two(callback) {
    await new Promise(res => {
        setTimeout(() => {
            res();
        }, 2000);
    })
    callback();
}
Sign up to request clarification or add additional context in comments.

2 Comments

Earlier I was thinking we can do it without using promise or async but we have to use it. This is helpful though. Thank you.
@MohdAyazurRahmanShaikh You can simply use a callback like in your example if you want to avoid using Promises.
1

you can use sync function like this

function two() {//this function takes 2 seconds
  return new Promise(resolve => {
    setTimeout(() => {
      resolve('resolved');
    }, 2000);
  });
}
function one(){
    console.log("one");
}
async function asyncCall() {
  console.log('calling');
  const result = await two();
 one()
  // expected output: 'resolved'
}

asyncCall();

here is reference link

Comments

1

You can use Promises:

function one(){
    console.log(1);
}

function two(){
    return new Promise((resolve, reject) => {
       setTimeout(()=>{
         console.log(2);
         resolve()
      },2000);
    })
}

two().then(() => one())

Comments

0

Put callback(); inside setTimeout:

function one(){
    console.log(1);
}
function two(callback){
    setTimeout(()=>{
        console.log(2);
        callback();
    },2000);
    
}
two(one);

2 Comments

I have mentioned it in question that I can do this if I put callback() inside setTimeout but what if I don't use setTimeout and still that particular code is taking 2-3 second like while we call api, it might take longer time.
@MohdAyazurRahmanShaikh let's say you will end up using request, you will have a success callback wherein you can perform any action AFTER the execution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.