I'm surprised that you got Promises working without any library. You are
probably using a very modern browser.
Promises aside, you have to provide a callback to your function. Then, on
the callback you perform the required operations.
/*
* Run the macro
*/
exports.run = function(url, callback) {
var url = url || "None";
[ ........ I REMOVED AJAX CODE .........]
ajax('./test-2.php?url='+url).then(function(result) {
console.log('**** > '+result);
callback(null,result); // it is a convention tu return the error
in first place, and then the results. We don't get any error, so put null
in place
}).catch(function(error) {
console.log('Error');
callback(error)
});
};
As you can see, I am not returning anything, I'm just calling the callback
with the response. Then you should use it like this:
//this function will handle the results
function my_ResultHandler(err,result){
if(err){
return; //finalize execution, we got an error. You are already
handling this on the promise, so no need for an extra handling here
}
//doo whatever you want with your result here, maybe creating a new
tiddler?
tw.addTiddler({title:'ajaxResult', text:result})
}
//here you call your macro, widget or whatever
url('my/url',my_ResultHandler);
Since you are using promises already you can return the promise on your url
code (like you do on the ajax function) which is more cleaner:
//This time we don't need a callback because we are using promises
exports.run = function(url) {
var url = url || "None";
[.... I removed code here because it is the same ....]
return ajax('./test-2.php?url='+url).catch(
function(err) {
console.log('Error!', err);
});
// you can handle errors, but it is not mandatory, you can just return the
promise, so code above can become
// return ajax('./test-2.php?url='+url)
}
function my_ResultHandler(result){....} // we don't need the error here,
because that is handled inside the .catch() statement.
//then calling it
url('my/url').then(my_ResultHandler);
hope this helps
--
You received this message because you are subscribed to the Google Groups
"TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/tiddlywiki.
To view this discussion on the web visit
https://groups.google.com/d/msgid/tiddlywiki/117bda2e-06d7-4a1b-8f7f-3c0e32f6c2a2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.