From 05db5f4b2dc9dd76aacc2e3ff1b5064db0479b0a Mon Sep 17 00:00:00 2001 From: Jeff Cressman Date: Sun, 3 Apr 2016 17:22:40 +0930 Subject: [PATCH] Adds section on promises and callbacks --- README.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/README.md b/README.md index 23f7cf9..9cecbf5 100644 --- a/README.md +++ b/README.md @@ -462,3 +462,25 @@ function putLeftFootIn() { } // ... etc ``` + +Prefer returning promise or callback but not both + +*Why?*: `strong-remoting` freaks out if we return a callback AND a promise see [this](https://gist.github.com/pulkitsinghal/8b74ece52975424ff2b9) and [this](https://groups.google.com/forum/#!topic/loopbackjs/QQfDSNT0DMA) for details. + +```javascript +/* Avoid */ +.then(function(result) { + cb(null, result); + return Promise.resolve(result); +}); +``` + +```javascript +/* Prefer */ +.then(function(result) { + if(cb) { + return cb(null, result); + } + return Promise.resolve(result); +}); +```