2

I want to know if we can implement a function which can block in Javascript. For example:

var username = getUserName(); //wait for user input username in a certain way
doSth(username);

I hope getUserName can block until a valid username is input by the user. I am wondering if we could use coroutine or other ways to achieve this goal. Thank you.

3
  • 3
    You could use the Javascript prompt() function. Commented Feb 3, 2014 at 4:42
  • How do you want the input from the user? Commented Feb 3, 2014 at 4:42
  • In some browsers, you can have a modal input with non-standard showModalDialog. I would not recommended doing this, though. Commented Feb 3, 2014 at 4:45

4 Answers 4

1

In the browser, the closest you are going to get to coroutines are the Python-like generators proposed in the Ecmascript 6 standard. However, only Firefox and the bleeding edge versions of NodeJs natively support them as of today.

Given that most browsers don't support coroutines (and won't for a long time because of old versions of IE), your best bet is using a pure-Javascript control-flow library (seee "async.js" or promises) or use some tool that compiles a dialect of Javascript extended with coroutines back into continuation-passing-style. There are a couple that use the ES6 syntax and some others that use different syntaxes (and there are also some non-JS languages compiling to Javascript)

Finally, to keep things complete, in Nodejs has a Fibers feature that is a bit similar to coroutines but its server-side-only so I don't think they are going to be very useful to you.

Sign up to request clarification or add additional context in comments.

Comments

0

This isn't "blocking" but you could always use a callback

function getUsername(done) {
  try {
    var username = // some process that gets the username;
    return done(null, username);
  }
  catch (err) {
    return done(err);
  }
}

getUsername(function(err, username) {
  if (err) throw err;
  console.log(username);
}

Comments

0

Whith the arriving of generators to javascript and the help of Aryn is possible to use coroutines and CSP(Communicating Sequential Processes) in Node.js or in the client-side.

Aryn + Promise example in Node.js:

function getUsername(id) {
    return new Promise(function(resolve, reject) {
        db.query('select username from user where id = ?', [id], function(r){
            resolve(r[0].username)
        })
    })
}

// your coroutine
aryn.run(function*(){
    var username = yield getUsername(id)
    console.log(username)
})

Using Aryns drive function:

db = aryn.drive(require('db'))

var getUsername = aryn.def(function*(){
    return yield aryn.receive(db.query('select username from user where id = ?', [id]))
})

aryn.run(function*(){
    var username = yield getUsername(id)
    console.log(username)
})

Comments

0

A CSP fan here. I spent like half a year working on CSP in JavaScript solution and I think I'm somewhere with this. If you are interested check Riew.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.