Live data from Hacker News

Write logic, not mechanics

jeditoolkit.com

21–30 of 41 posts

Re: Write logic, not mechanics

#21
post #9

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.

I mainly was referring to JS where we don't have coroutines and there is no plans to have them either.

Re: Write logic, not mechanics

#22

This is one of the simplest implementations of deferreds I've seen in JS so far.

You just have to write function per operation that is it:

    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

#23

The 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

#24
post #23

The 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

Edit: cool, it works!

Re: Write logic, not mechanics

#25
post #23

The 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

[deleted]

Re: Write logic, not mechanics

#27

I 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.

You're absolutely right! My English is far from being perfect and in addition I was rushing to get this post out making bunch of mistakes. I tried to fix all the "than" "then" issues, thanks for pointing that out ;)

Re: Write logic, not mechanics

#28
post #23

The 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

The typoed syntax was nicer than the real syntax, unfortunately.

Re: Write logic, not mechanics

#30
post #23

The 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

You were writing an article about it and didn't get it right on the first time - maybe that's the reason more people don't use it.
Post reply on HN