简体   繁体   中英

Can't assign a value to the variable

var readline = require('readline');
var test
rl = readline.createInterface(process.stdin, process.stdout);
rl.question('What is your favorite food?', function(answer) {
 console.log('Oh, so your favorite food is ' + answer);
 test = answer
});
rl.close()

webdriverio
 .remote(options)
 .init()
 .url('http://www.google.com')
 console.log(test) //undefined, why?

So I don't get it why it shows me "undefined" instead of the string I enter thru terminal. Why is that?

My guess is this is not sync, but how do I make it sync?

  1. Installed: https://www.npmjs.com/package/readline-sync
  2. Modified the code:

     var readlineSync = require('readline-sync'); var test webdriverio .remote(options) .init() .url('http://www.google.com').then(function() { test = readlineSync.question('What is your favorite food?'); //Honeyberry console.log(test) //Honeyberry }) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM