I am trying to listen to a "message", by adding an event listener and based on an eventName
property in the event data which is coming as string, I need to make a promise and fetch some data from an API.
But this promise call is not happening.
Sample data:
event.data = `{"eventName":"modal:open","eventData":{"state":"redeem","stateParams":{"rewardTitle":"Lab Coat Live - 35% Discount","rewardId":550288},"width":700,"src":"https://qa-crowdtwist.thermofisher.com/widgets/t/redeem/550288/#2"},"direction":"up","widgetId":13324,"version":2}`
If "eventName": "reward:redeemed"
then I need to make the API call. But the promise is not executing neither throwing any error. Am I doing something wrong or is there any other procedure to make this promise to work?
const event = new Event("message");
$window.addEventListener(event.type, function (event) {
if(typeof event.data === "string"){
var data = event.data;
var redeemed = false;
if(data.indexOf("reward:redeemed")){
redeemed = true;
}
}
if(redeemed){
dataService.getUserPoints();
}
}, false);
$window.dispatchEvent(event);
question from:
https://stackoverflow.com/questions/66051994/event-listener-message-based-on-event-name-making-an-api-call-with-promise-in 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…