API 的 Javascript 效率 [英] Javascript effiency with APIs

查看:18
本文介绍了API 的 Javascript 效率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我写的代码是否有效(我得到了我想要的),但它看起来并不吸引人.有没有更好的方法来写这个?

I was wondering if the code I have written is efficient (I am getting what I want), but it doesn't look appealing. Is there a better way to write this?

require('es6-promise').polyfill();
require('isomorphic-fetch');

function get_height() {

  const height_url = 'https://somewebsite.info/api/someth/tip/height';

  var data = fetch(height_url)
    .then((resp) => resp.json())
    .then(function(data) {
      console.log("The current height is " + data)
    })
}

get_height()

推荐答案

你可以使用 async/await

you can use async/await

async function get_height() {

    const height_url = 'https://somewebsite.info/api/someth/tip/height';

    var data = await (await fetch(height_url)).json();

    console.log("The current height is " + data)
}

这篇关于API 的 Javascript 效率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆