-1

I'm trying to trigger 3 functions one after another. When each completes, next starts my code :-

getInfo(f1 , f2).then(function() {
    setInfo().then(function(callback) {
        Three();
    });
});

i get this error :-

>TypeError: Cannot read property 'then' of undefined

Update

function setInfo(){  

    alert('done')   

  } 
Prince Hamza
  • 1,090
  • 12
  • 21
  • 1
    or either getInfo or setInfo (or both!) are not defined in the scope you trying to access it. Try to share a little bit more of code to us to see what is the issue. – iwaduarte Jun 08 '18 at 17:43
  • need to know more about `setInfo()` – Omar Jun 08 '18 at 17:44
  • What does `getInfo()` return? What does `setInfo()` return? Why aren't you returning from within either `then` callback to continue the chain? – zero298 Jun 08 '18 at 17:46
  • 2
    The functions `getInfo` and `setInfo` must return a `Promise` – Karthik Jun 08 '18 at 17:48
  • 1
    What are you actually trying to do? [`alert`](https://developer.mozilla.org/en-US/docs/Web/API/Window/alert) is blocking and doesn't return a Promise. You can't call `then()` on something that doesn't have a `then()` method. – zero298 Jun 08 '18 at 18:02

1 Answers1

2

You need to actually chain.

Let's assume your code has an API similar to below. getInfo, setInfo, and maybe Three are all asynchronous. They return Promises that resolve to some value.

function getInfo(a, b) {
  return Promise.resolve(true);
}

function setInfo() {
  alert("done");
  return Promise.resolve(true);
}

function Three() {
  return Promise.resolve(true);
}

You need to actually chain them together so they all wait on one another and propagate up values.

const f1 = "foo";
const f2 = "bar";

getInfo(f1, f2)
  .then(() => setInfo())
  .then(cb => Three());
zero298
  • 25,467
  • 10
  • 75
  • 100