I am using DOJO 1.10.4, I needto run a method after topics a,b,c
are broadcasted. Something similarly to promise dojo/promise/all
.
Does DOJO have this feature, any idea how to solve it?
Module A
topic.publish('a');
Module B
topic.publish('b');
Module C
topic.publish('c');
Dojo doesn't have anything built in for this, but it is pretty trivial to build something that you can even then use with dojo/promise/all
which you mention.
function createTopicPromise(topicName) {
var dfd = new Deferred(); // dojo/Deferred
var handle = topic.subscribe(topicName, function (someValue) {
handle.remove();
dfd.resolve(someValue);
});
return dfd.promise;
}
all([
createTopicPromise('a'),
createTopicPromise('b'),
createTopicPromise('c')
]).then(function (values) {
// ...
});
Keep in mind that pub/sub inherently doesn't queue publishes for subscribers - that is, if a topic gets published prior to a subscriber being hooked up for it, that publish will go completely unnoticed. Topics can also be published multiple times, while the approach above listens for only the first publish subsequent to calling createTopicPromise
(since promises only resolve or reject once).