prismicio / javascript-kit

Development kit for the Javascript language
https://developers.prismic.io
106 stars 69 forks source link

submit() method should returns a Promise. #71

Closed ggrossetie closed 9 years ago

ggrossetie commented 9 years ago

I think it would be great if the submit() method returns a Promise. In my case I need to query multiple documents to compose my page (the two latest news, the page content, a popular member, etc...) and my code is a callback hell !

ctx.api.form('everything').ref(ctx.ref).query('firstQuery').submit(function (err, docs) {
  if (err) { prismic.onPrismicError(err, req, res); return; }

  ctx.api.form('everything').ref(ctx.ref).query('secondQuery').submit(function (err, docs) {
    if (err) { prismic.onPrismicError(err, req, res); return; }

    ctx.api.form('everything').ref(ctx.ref).query('thirdQuery').submit(function (err, docs) {
        if (err) { prismic.onPrismicError(err, req, res); return; }
        res.render('', {});
    });
  });
});

But will look much better with Promise:

var firstQuery = ctx.api.form('everything').ref(ctx.ref).query('firstQuery').submit();
var secondQuery = ctx.api.form('everything').ref(ctx.ref).query('secondQuery').submit();
var thirdQuery = ctx.api.form('everything').ref(ctx.ref).query('thirdQuery').submit();

firstQuery.then(secondQuery).then(thirdQuery).then(new function(result) {
  res.render('', {});
});

Maybe I'm missing something with the API ? Or my use case is an edge case ?

Thanks, and great product by the way (a pleasure to develop a website with :+1:)

masterots commented 9 years ago

Not an edge case, but in the meantime, since they return a node-style callback, you can use a library like Q to wrap those function calls to act like promises.

guillaumebort commented 9 years ago

Yes, I agree it is better with Promises, but since there is no standard Promise implementation available everywhere yet, it would mean introduce an extra dependencies.

As @masterots says, in the meantime it is possible to wrap callback using Q.

ggrossetie commented 9 years ago

Yes that's true, that would introduce an extra dependencies... do you know if a standard Promise implementation will come anytime soon ?

Thanks for you help, I ended up using https://github.com/kriszyp/node-promise but didn't work out so well. I will give Q a try.

azappa commented 9 years ago

Any chance (or tutorial) to integrate Prismic with KoaJS (or generators, using --harmony)?

erwan commented 9 years ago

Hi azappa,

We only provide starter kit for the most popular frameworks, which is why for Node.js we have an Express.js starter: https://github.com/prismicio/javascript-nodejs-starter

That said the kit should work with any framework, if you have any problem to integrate it with KoaJS don't hesitate to reach us either though a support ticket or here.