How do I convert an existing callback API to promises?

I want to work with promises but I have a callback API in a format like: 1. DOM load or other one time event: window.onload; // set to callback … window.onload = function() { }; 2. Plain callback: function request(onChangeHandler) { … } request(function() { // change happened … }); 3. Node style callback (“nodeback”): … Read more

How can I pass a parameter to a setTimeout() callback?

I have some JavaScript code that looks like: function statechangedPostQuestion() { //alert(“statechangedPostQuestion”); if (xmlhttp.readyState==4) { var topicId = xmlhttp.responseText; setTimeout(“postinsql(topicId)”,4000); } } function postinsql(topicId) { //alert(topicId); } I get an error that topicId is not defined Everything was working before I used the setTimeout() function. I want my postinsql(topicId) function to be called after some … Read more

How to access the correct `this` inside a callback

I have a constructor function which registers an event handler: function MyConstructor(data, transport) { this.data = data; transport.on(‘data’, function () { alert(this.data); }); } // Mock transport object var transport = { on: function(event, callback) { setTimeout(callback, 1000); } }; // called as var obj = new MyConstructor(‘foo’, transport); However, I’m not able to access … Read more