Promise chaining with Q nodejs - promise

I have this code here but I cannot figure out why the second FS.readfile() executes last. Basically I want to:
readFile(passwd)
.then(console(textpasswd)
.then(readFile(hosts).
.then(console(texthosts);
But what happens is the readFile(hosts) executes last in the chain. I sense something is wrong with my deferring, but what?
Here is the code
module.paths.push('/usr/local/lib/node_modules');
var Q = require('q');
var FS=require('fs');
var deferred = Q.defer();
var p=deferred.promise;
FS.readFile("/etc/passwd", "utf-8", function (error, text) {
if (error) {
deferred.reject(new Error(error));
} else {
deferred.resolve(text);
}
});
p.then(function (text) {
console.log(text);
}).then(FS.readFile("/etc/hosts", "utf-8", function (error, text) {
if (error) {
deferred.reject(new Error(error));
} else {
deferred.resolve(text);
}
})
).then(function (text) {
console.log(text);
});

The deferred must obtain a new promise to continue the chain correctly. Also notice the first (function())() must be executed to return a promise although you don't need to do it this way. So here is my solution. (I cleaned it up a little):
module.paths.push('/usr/local/lib/node_modules');
var Q = require('q');
var FS = require('fs');
(function () {
var deferred = Q.defer();
FS.readFile("/etc/passwd", "utf-8", function (error, text) {
if (error) {
deferred.reject(new Error(error));
} else {
deferred.resolve(text);
}
})
return deferred.promise;
})().then(function (text) {
console.log(text);
}).then(function () {
var deferred = Q.defer();
FS.readFile("/etc/hosts", "utf-8", function (error, text) {
if (error) {
deferred.reject(new Error(error));
} else {
deferred.resolve(text);
}
})
return deferred.promise;
}).then(function (text) {
console.log(text);
}).then(function () {
console.log('The End');
});

Related

Leaflet mapping: Assign object to fetch promise for local GeoJSON file

I am looking to assign as an object a Fetch API promise from a local GeoJSON file.
Here is the code
fetch("data/sites.geojson")
.then(function(response) {
return response.json();
})
.then(function(data) {
L.geoJSON(data, {
pointToLayer: styles_sites
}).addTo(map);
});
};
I tried the call back method, as advised here
Saving fetched JSON into variable
(EDIT) New code, but there is still a missing formal parameter
function getData("data/sites.geojson", cb) {
fetch("data/sites.geojson")
.then(function(response) {
return response.json();
})
.then(function(data) {
L.geoJSON(data, {
pointToLayer: styles_sites,
onEachFeature: function (feature, layer) {
layer.on('mouseover', function() {
layer.openPopup(layer.bindPopup("<b>"+feature.properties.nombre+"</b>"))
});
layer.on('mouseout', function() {
layer.closePopup();
});
layer.on('click', function () {
layer.bindPopup("<b>Nombre: </b>"+feature.properties.nombre+"<br><b>Barrio: </b>"+feature.properties.barrio+"<br><b>Tipo: </b>"+feature.properties.tipo+"<br><b>Ubicacion: </b>"+feature.properties.ubicacion+"<br><b>Correo: </b>"+feature.properties.contacto);
});
}
}).addTo(map);
.then(function(result) {
cb(result);
});
});
};
getData("data/sites.geojson", function (data) {
return console.log({data});
});
Most probably just incorrect syntax of your callback function:
// Use either arrow function
getData("data/sites.geojson", (data) => {
return console.log({data});
});
// or standard function
getData("data/sites.geojson", function (data) {
return console.log({data});
});
I found the way to work this out by adding within the fetch function, what I originally wanted to do on the map.
This was to add a L.controlLayer using the geojson as overlay.
This is the code that made it work:
let sites = getData()
.then((function(data) {
L.geoJSON(data, {
pointToLayer: styles_sites,
onEachFeature: function LayerControl(feature, layer) {
var popupText = "<b>" + feature.properties.nombre + "<br>";
layer.bindPopup(popupText);
category = feature.properties.tipo;
// Initialize the category array if not already set.
if (typeof categories[category] === "undefined") {
categories[category] = L.layerGroup().addTo(map);
layersControl.addOverlay(categories[category], category);
}
categories[category].addLayer(layer);
layer.on('mouseover', function() {
layer.openPopup(layer.bindPopup("<b>"+feature.properties.nombre+"</b>"))
});
layer.on('mouseout', function() {
layer.closePopup();
});
layer.on('click', function () {
layer.bindPopup("<b>Nombre: </b>"+feature.properties.nombre+"<br><b>Barrio: </b>"+feature.properties.barrio+"<br><b>Tipo: </b>"+feature.properties.tipo+"<br><b>Ubicacion: </b>"+feature.properties.ubicacion+"<br><b>Correo: </b>"+feature.properties.contacto);
});
}
}).addTo(map);
}));
Actually it comes from one of your answer on another post ghybs.

Nodejs - xmlhttprequest promise never finish

module.js
var Promise = require('bluebird');
var XMLHttpRequest = require('xhr2');
function fetchdata(id) {
var url = 'http://www.youtube.com/watch?v=' + id;
return new Promise(function (fulfill, reject) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.onload = function() {
var jsonStr;
try {
fulfill(xhr.response);
} catch (e) {
reject(jsonStr);
}
};
xhr.onerror = function(e) {
reject(e);
};
xhr.send('');
});
}
module.exports = {
getdata: function (videoID) {
return new Promise(function (fulfill, reject) {
if (!videoID) {
reject(new Error('Unable to get video id.'));
return;
}
fetchdata(videoID).then(
function (d) {
console.log( d);
}
);
});
}
};
index.js
var parser = require('./module.js');
parser.getdata("ZI4tRn4dOGg", function (data) {
console.log(data);
}
)
I tried to get youtube view page source code with xmlhttprequest.
BUT above code does not finish. I guess it is waiting for something.
Is problem from bluebird? or xhr2? and why does this code never finish?
Your xhr instance had a memory leak, might be a problem with the library, last publish was a year ago. Bluebird was ok. You can fix the hangup by using node-fetch and dropping in this replacement for fetchdata
const fetch = require('node-fetch')
function fetchdata(id) {
var url = 'http://www.youtube.com/watch?v=' + id;
return fetch(url).then(res => res.text())
}

The on `data` or `error` is never fired on createKeyStream level

I have the following code to return all the keys in the level DB in Node.js app, this is a method in a ES6 class:
class LevelDB {
constructor() {
this.db = level(chainDB);
}
getAllItems() {
let self = this;
return new Promise(function(resolve, reject){
self.db.createKeyStream()
.on('data', function (data) {
resolve(data);
})
.on('error', function (err) {
reject(err)
});
}
}
The database is created successfully.
What I'm notice is that the on data or on error is never fired? Im new using level so any help will be appreciate.
This solve the issue;
I just realize that I need to wait for the Stream close:
class LevelDB {
constructor() {
this.db = level(chainDB);
}
getAllItems() {
let self = this;
let dataArray = [];
return new Promise(function(resolve, reject){
self.db.createKeyStream()
.on('data', function (data) {
dataArray.push(data);
})
.on('error', function (err) {
reject(err)
})
.on('close', function () {
resolve(dataArray);
})
}
}

readTextAsync WinJS cannot work

I need to read content from file. I have global variable fileDate and I want to put content to this variable, but when I call load method, the variable is undefined.
var filename = "dataFile.txt";
var fileDate;
WinJS.UI.Pages.define("index.html", {
ready: function (element, options) {
loadDate();
console.log("main" + fileDate);
this.fillYearSelect();
},
function loadDate() {
return localFolder.getFileAsync(filename).then(function (file) {
return Windows.Storage.FileIO.readTextAsync(file).then(function (fileContent) {
fileDate = fileContent;
console.log("fileContent " + fileContent);
},
function (error) {
console.log("Błąd odczytu");
});
},
function (error) {
console.log("Nie znaleziono pliku");
});
}
Sorry for my English :)
Don't forget that javascript is asynchronous, when you call console.log("main" + fileDate), the method loadDate() is not finished, and that is why your fileDate is not defined (yet).
You could use promises to achieve this.
Here is an example based on your code :
var filename = "dataFile.txt";
var fileDate;
var applicationData = Windows.Storage.ApplicationData.current;
var localFolder = applicationData.localFolder;
function loadDate() {
return new Promise(function (onComplete, onError) {
localFolder.getFileAsync(filename).then(function (file) {
Windows.Storage.FileIO.readTextAsync(file).then(function (fileContent) {
fileDate = fileContent;
console.log("fileContent " + fileContent);
onComplete(fileDate);
},
function (error) {
console.log("Error on readTextAsync");
onError(error);
});
},
function (error) {
console.log("Error on getFileAsync");
onError(error);
});
});
}
Now loadDate()returns a promise, you can now use .then() method to do things when loadDate() is finished.
loadDate().then(function (fileDate) {
console.log("Content : " + fileDate);
},
function (error) {
console.log(error);
});

Cannot return the value from my function

I could not return the value from the function, I have no problem with my sql it works fine, I just can't return the result value and pass it to res variable.
io.on('connection', function (socket) {
socket.on('getdetails', function (data) {
var res = getdatatodb(data.id);
console.log("result",res);
});
});
});
function getdatatodb(id){
connection.connect(function(err) {
if (err) {
console.error('error connecting: ' + err.stack);
return;
}
var options = {sql: ".....",nestTables: true };
connection.query(options,function(err, results) {
if(err){
console.log(err);
}
return results;
});
});
}
Thank you in advance.

Resources