2

I want to run some of my Parse Cloud Code on Heroku (Node) to kick start the migration process out of Parse's hosted Cloud Code.

One of the differences between the two environments is the fact that Parse SDK you use is different too. On node, I found that if you do a

var Parse require('parse/node').Parse;

... then Parse.Cloud.httpRequest is actually missing.

I was looking for Node alternatives for performing http requests and found request. But... it doesn't really fit in well with Parse Promises:

parsePromise1()
    .then(parsePromise2)     // okay
    .then(requestPromise)    // CUSTOM HTTP REQUEST!
    .done(parsePromise3)     // this Parse promise always succeeds, and occurs even before the request is fully completed.

I was wondering if anybody had found better ways of converting out of Parse Promises? Thanks.

fatuhoku
  • 4,815
  • 3
  • 30
  • 70

2 Answers2

3

You can use the request-promise package that wraps request with promises support or a library like bluebird that exposes a method that converts callback APIs to promises.

I recommend that you migrate from Parse promises which are suboptimal to ES2015 native promises (or, more powerful alternatives like bluebird). With those you could:

Promise.resolve(someParsePromise).then(() => // convert
     return someParsePromiseFn()// return some Parse promise, that works
}).then(res => {
     // more async work ...
});

And avoid .done altogether.

Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
  • Hmm! That's a a great idea. Just gotta wrap the first one. This obviates the need to patch Parse.Promise which feels really hacky. I'll give it a go. – fatuhoku Jan 31 '16 at 22:33
  • 1
    `Promise.resolve` is designed in such a way that Parse promises are safely consumed by `Promise.resolve`. – Benjamin Gruenbaum Jan 31 '16 at 22:34
0

Its not ideal at all, but you can use this kind of wrapper from native Bluebird to native promises:

    const Promise = require('bluebird')

    function handle () {
          return new Promise((resolve, reject) => {
           handleWithNativePromise(...arguments)
            .then(resolve)
            .catch(reject)
          })
    }
Liran Brimer
  • 3,418
  • 1
  • 28
  • 23