it’s possible to make sync function async it’s not the case other way round Is this terminology correct? I thought it is easier to make async functions sync by blocking it, and harder to unblock a sync one. Am I confusing this with something else?
I think the OP refers to the syntax/API for each. If you're already using async APIs, adding a sync one that runs in a separate thread won't change much. On the other hand, you need something like CPS or coroutines to cleanly use an async function in a program mostly using sync APIs. Of course, if you have the right tools (CPS/coroutines) you can make any async API look sync, as you said.
Write logic, not mechanics
21–30 of 41 posts
Re: Write logic, not mechanics
#22This is one of the simplest implementations of deferreds I've seen in JS so far.
var auth = promised(function(user, data) {
var isAdmin = user.role === admin
return isAdmin ? render(open("yay.xml"), db.send(data.request))
: render(open("no_permissions.xml"), {})
})
var puser = getUser(data.session)
return auth(user, data)Re: Write logic, not mechanics
#23The code doesn't actually work though, the very first example is broken: function sum(a, b) { return a + b } sum = promised(sum) var a = defer() // make promise var b = sum(a, 1) var b = sum(b, 5) console.log(b) // eventually prints => 17 a.resolve(11) // fulfill promise Prints: [object Object]15
var deferred = defer() // make promise
var a = deferred.promise
var b = sum(a, 1)
var c = sum(b, 5)
console.log(c) // eventually prints => 17
deferred.resolve(11) // fulfill promiseRe: Write logic, not mechanics
#24The code doesn't actually work though, the very first example is broken: function sum(a, b) { return a + b } sum = promised(sum) var a = defer() // make promise var b = sum(a, 1) var b = sum(b, 5) console.log(b) // eventually prints => 17 a.resolve(11) // fulfill promise Prints: [object Object]15
I have already fixed that, it was a typo, I meant following instead: var deferred = defer() // make promise var a = deferred.promise var b = sum(a, 1) var c = sum(b, 5) console.log(c) // eventually prints => 17 deferred.resolve(11) // fulfill promise
Re: Write logic, not mechanics
#25The code doesn't actually work though, the very first example is broken: function sum(a, b) { return a + b } sum = promised(sum) var a = defer() // make promise var b = sum(a, 1) var b = sum(b, 5) console.log(b) // eventually prints => 17 a.resolve(11) // fulfill promise Prints: [object Object]15
I have already fixed that, it was a typo, I meant following instead: var deferred = defer() // make promise var a = deferred.promise var b = sum(a, 1) var c = sum(b, 5) console.log(c) // eventually prints => 17 deferred.resolve(11) // fulfill promise
Re: Write logic, not mechanics
#26Re: Write logic, not mechanics
#27I will be down voted for saying so, but "than" and "then" seem to be reversed throughout the entire article. I would like to learn more about promises now.
Re: Write logic, not mechanics
#28The code doesn't actually work though, the very first example is broken: function sum(a, b) { return a + b } sum = promised(sum) var a = defer() // make promise var b = sum(a, 1) var b = sum(b, 5) console.log(b) // eventually prints => 17 a.resolve(11) // fulfill promise Prints: [object Object]15
I have already fixed that, it was a typo, I meant following instead: var deferred = defer() // make promise var a = deferred.promise var b = sum(a, 1) var c = sum(b, 5) console.log(c) // eventually prints => 17 deferred.resolve(11) // fulfill promise
Re: Write logic, not mechanics
#29 return Mustache.render(template, data)
Isn't exactly equivalent to the non-promise one, since now `Mustache.render` is async, is it?Re: Write logic, not mechanics
#30The code doesn't actually work though, the very first example is broken: function sum(a, b) { return a + b } sum = promised(sum) var a = defer() // make promise var b = sum(a, 1) var b = sum(b, 5) console.log(b) // eventually prints => 17 a.resolve(11) // fulfill promise Prints: [object Object]15
I have already fixed that, it was a typo, I meant following instead: var deferred = defer() // make promise var a = deferred.promise var b = sum(a, 1) var c = sum(b, 5) console.log(c) // eventually prints => 17 deferred.resolve(11) // fulfill promise