1

context :

i need to call a function (function A) to make HTTP request two time, after this, i need to call another fonction (function B) that compute the two array

the normal order should be

  • (Function A & Function A) asynchronously
  • when the 2 other are done, Function B

so i turned myself to promise, but here is the issue, i can't figured out how to syntax it properly, i use Coffeescript.

here's what i got so far, but right now it's not working

myCoolPromise = () ->
  return new Promise (resolve, reject) ->
  postRequest(diagUrl, diagnosisBody, storesConnectionObject)
  postRequest(storesUrl, brandBody, storesObject)
  success = true
  if success
    resolve 'stuff worked'
  else
    reject Error 'it broke'


myCoolPromise(storesObject.storesArray, storesConnectionObject.storesArray, absentObject).then (response) ->
  console.log 'success', response
  handleResult(storesObject.storesArray, storesConnectionObject.storesArray, absentObject)
.catch (error) ->
  console.error 'failed', error
Mehdi S.
  • 471
  • 1
  • 4
  • 18
  • I don't get what `myCoolPromise` is supposed to do. It doesn't seem to be asynchronous at all, and `success` is always true, so you shouldn't use promises here? – Bergi Jun 20 '17 at 13:28

2 Answers2

0

i answer my own question to keep a trace in StackOverflow

my problem was syntax side

i changed postRequest to return a promise, resolve if the request get back with statuscode 200 or reject if error, and chained the two function in a promise like this :

  myCoolPromise = (diagUrl, diagnosisBody, storesUrl, brandBody) ->
    return new Promise (resolve, reject) ->
      postRequest(diagUrl, diagnosisBody)
        .then (storesConnectionObject) ->
          postRequest(storesUrl, brandBody)
            .then (storesObject) ->
              resolve handleResult(storesObject, storesConnectionObject)
            .catch (error1) ->
              reject error1
        .catch (error2) ->
          reject error2

i'm still open to a better way to resolve this, thanks

Mehdi S.
  • 471
  • 1
  • 4
  • 18
  • Avoid the [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it)! – Bergi Jun 20 '17 at 13:26
  • 1
    Just `return Promise.all(postRequest(…), postRequest(…)].then(handleResult)`? – Bergi Jun 20 '17 at 13:27
-1

There are a number of issues with this code, so I'll just go one by one.

First of all, you're using this code to invoke the function:

myCoolPromise(storesObject.storesArray, storesConnectionObject.storesArray, absentObject).then (response) ->

In order for this to work, your myCoolPromise method would need to take 3 arguments (right now it takes 0).

Second, in Coffeescript the indentation after the new Promise line is absolutely necessary. Right now you have this:

  return new Promise (resolve, reject) ->
  postRequest(diagUrl, diagnosisBody, storesConnectionObject)

Because the following lines aren't indented, anything after that first line is not considered part of the promise, and your myCoolPromise method returns an empty promise.

Now finally, onto the content of the promise (the async call itself). You haven't shown how postRequest is defined, but the way you're calling it, it does not seem to be async. Keep in mind that, besides ES7 await, promises, and cpu-hogging loops, the only way Javascript supports async is calbacks. So, if postRequest has a callback api, you supply a callback with the incovation and inside the callback body, invoke the resolve or reject. On the other hand, if postRequest offers a promise api, you can chain on .then -> resolve("success") and such

max pleaner
  • 26,189
  • 9
  • 66
  • 118
  • my `postRequest` function is structured like this `postRequest =(postUrl, postBody, postArray) ->` i don't understand why `handleresult` is still trigger first now, the 3 function are invoked but in the same order than without promise, and console.log 'stuff worked' are not shown for exemple – Mehdi S. Jun 20 '17 at 09:00