Re:JavaScript Promises: There and back again
by ihaveqvestion on Tuesday December 17, 2013 @12:54PM
(#6922525)
In the submitted article, two callback methods are usually provided (one for success and another for failure). Is that possible when using generators? If so, what does that look like?
[ Parent
| on HN ]
Re:JavaScript Promises: There and back again
by sciolistse on Tuesday December 17, 2013 @01:18PM
(#6922717)
In generator code you'd use try/catch to handle failure instead.
try {
var user = yield Users.get(1234);
return user.name;
} catch(ex) {
// do whatever
}
rather than.. return Users.get(1234).then(
function (u) { return u.name; },
function (ex) { /* do whatever */ }
);
Re:JavaScript Promises: There and back again
by spion on Tuesday December 17, 2013 @07:20PM
(#6924964)
Those are not entirely equivalent. For example, if you make a typo when writing "user" or "u", the first one will catch that typo (cannot get property name of undefined) while the second one will not.
Users.get(1234)
.then(u => u.name)
.catch(ex => 1/* do whatever */);