Last active
September 28, 2017 14:25
-
-
Save drdrsh/10779e1a6b1d0ae7779a17690a334ca3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const Promise = require("bluebird"); | |
const readline = require('readline'); | |
const rl = readline.createInterface(process.stdin, process.stdout, null); | |
//Some dummy data | |
const records = [{ | |
'title': null, | |
'lat' : 9.1111, | |
'lng' : 5.1111, | |
'desc' : 'A record missing a title' | |
},{ | |
'title': "Not missing data", | |
'lat' : 9.1111, | |
'lng' : 5.1111, | |
'desc' : 'Some description' | |
},{ | |
'title': null, | |
'lat' : 9.1111, | |
'lng' : 5.1111, | |
'desc' : 'Another record missing a title with unicode chars (هذا نص عربي)' | |
}]; | |
//This function when "await"ed should act as javascript blocking "prompt" | |
async function getUserInput(missing_record) { | |
return new Promise( (resolve, reject) => { | |
rl.question(`Missing data: ${missing_record.desc}, please input value \n`, (line) => { | |
resolve(line); | |
}); | |
}); | |
} | |
//Loop through our data and prompt the user when records with empty title are encountered | |
async function processData() { | |
for(const record of records) { | |
if(!record.title) { | |
//Execution will wait for user input and store it in record.title | |
record.title = await getUserInput(record); | |
} | |
console.log(`Record title is ${record.title}`); | |
} | |
//Exit | |
rl.close(); | |
process.stdin.destroy(); | |
} | |
processData(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment