Leo Optimo II
Leo Optimo II

Reputation: 61

How to use if / else statement with a JSON fetch API response Javascript

I have two working code snippets that I would like to link with if/else logic. The first part of the code receives a JSON response from a web API. JSON.TEMPERATURE is the key variable needed.

const fetch = require("node-fetch");

const api_url = 'https://www.placeholder_URL.com/api/getTemperature'

fetch(api_url)
  .then((response) => {
    return response.json();
  })
  .then((myJSON) => {
    console.log(myJSON.TEMPERATURE); // **This returns a number between 8 and 15**
  });

The second part of the code plays a MIDI note. In the case below, note # 60 which is the Middle C. C# would be note 61, D note 62 and so forth.

let easymidi = require("easymidi")
let output = new easymidi.Output("mindiPort", true)

let sendNote = (noteValue, duration) => {
    output.send("noteon", noteValue)

    setTimeout(()=> {
        output.send("noteoff", noteValue)
    }, duration);
}

setInterval(() => {
    let noteValue = {
        note: 60,   // **need to change this number depending on JSON.TEMPERATURE**
        velocity: 100,
        channel: 1
    }
    sendNote(noteValue, 500)
}, 2000);

What I am looking to achieve is an if/else where the note played is dependent on JSON.TEMPERATURE.

Something like:

if JSON.TEMPERATURE == 8, play note 60

if JSON.TEMPERATURE == 9, play note 61

else, play note 55

I would also like to run the API request every 5 seconds.

Upvotes: 0

Views: 4208

Answers (2)

Besnik Korça
Besnik Korça

Reputation: 1094

You need to call the setInterval iniside of the .then of the fetch request, that way the note will be sent after the api request has finished and you have received the JSON response.

How you've currently implemented it it will call sendNote every 2 seconds. Additionally you want to do the api call every 5 seconds. This means that we want to clear the sendNote interval of the previous api call that's why the interval variable is there and we call clearInterval if interval is set.

Of course you can abstract it by putting some of this code into functions to make it look prettier but this is the gist of it

let interval;
const fetch = require("node-fetch");

const api_url = 'https://www.placeholder_URL.com/api/getTemperature'
setInterval(() =>
fetch(api_url)
   .then((response) => {
      return response.json();
   })
.then((myJSON) => {
   if (interval) {
     clearInterval(interval);
   }
   interval = setInterval(() => {
      let note;
      if (myJSON.TEMPERATURE == 8) {
        note = 60;
      } else if (myJSON.TEMPERATURE == 9) {
        note = 61;
      } else {
        note = 55;
      }
      let noteValue = {
         note: note,
         velocity: 100,
         channel: 1
       }
       sendNote(noteValue, 500)
   }, 2000);
 }), 5000);

Upvotes: 1

Jamiec
Jamiec

Reputation: 136164

Seems like all you need is an if statement such as

if(myJSON.TEMPERATURE == 8){
   note = 60;
}
else if(myJSON.TEMPERATURE == 9){
   note = 61;
}
else{
  note = 55;
}

So just put this in place of your console.log part.

const fetch = require("node-fetch");

const api_url = 'https://www.placeholder_URL.com/api/getTemperature'

fetch(api_url)
  .then((response) => {
    return response.json();
  })
  .then((myJSON) => {
    let note;
    if(myJSON.TEMPERATURE == 8){
       note = 60;
    }
    else if(myJSON.TEMPERATURE == 9){
       note = 61;
    }
    else{
      note = 55;
    }

    let noteValue = {
        note: note,   
        velocity: 100,
        channel: 1
    }
    sendNote(noteValue, 500)
  });

let sendNote = (noteValue, duration) => {
    output.send("noteon", noteValue)

    setTimeout(()=> {
        output.send("noteoff", noteValue)
    }, duration);
}

Upvotes: 2

Related Questions