Set value of cell/name in callback function

I’m trying to set the value of a cell named “restaurants” in a call back from Tabletop, but when I reference restaurants in another cell, I receive “RuntimeError: restaurants is not defined”.

Tabletop = require("https://cdnjs.cloudflare.com/ajax/libs/tabletop.js/1.5.1/tabletop.min.js")
  var publicSpreadsheetUrl = "https://docs.google.com/spreadsheets/d/1eh3Lhf1DzED8q62B5XXB0GZAwT3uzcxHJw2QD0ev9P4/edit?usp=sharing";

    Tabletop.init( { key: publicSpreadsheetUrl,
                     callback: showInfo,
                     simpleSheet: true } )

  function showInfo(data, tabletop) {
    console.log(data);
    let restaurants = data;
  }
} 

Thank you for the help.l

Tabletop uses a callback-style API which you’ll need to adapt to use promises. Here’s how you’d do that:

data = new Promise((resolve, reject) => {
  Tabletop.init({
    key: url,
    callback: data => {
      if (data) resolve(data);
      else reject(new Error("unable to load"));
    },
    simpleSheet: true
  })
})

And here’s the live notebook: https://beta.observablehq.com/@mbostock/tabletop-test

I’ll also add an example of adapting a callback API to promises in the Introduction to Promises notebook. I forgot to mention that, so thanks for the reminder!

1 Like

Perfect! Thank you very much.