Hi. I am wanting to retrieve a value from a JSON “file” returned via a web service and use it as the value of my macro’s “return.” I am struggling with how to “delay” the return statement until the value has had time to be fetched.
/*\
title: $:/jenn/code-bits/macros/getJSON.js
type: application/javascript
module-type: macro
retrieve values from a JSON web service
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
/*
Information about this macro
*/
exports.name = "getJSON";
exports.params = [
{name: "hex"}
];
/*
Run the macro
*/
exports.run = function(hex) {
let color_url = "https://www.thecolorapi.com/id?hex=" + hex;
let payload = "nope";
function reqListener () {
console.log(this.response);
payload = this.response;
}
let xhr = new XMLHttpRequest();
xhr.addEventListener("load",reqListener);
xhr.open('GET', color_url);
xhr.responseType = 'json';
xhr.send();
xhr.onload = function() {
let responseObj = xhr.response;
alert(responseObj.hsv.h);
};
return payload;
};
})();