如何在 PapaParse 中使用 Promises? [英] How to use Promises with PapaParse?

查看:18
本文介绍了如何在 PapaParse 中使用 Promises?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PapaParse 的 API 有一个异步回调函数.我想知道如何将其转换为承诺.例如:

PapaParse has an asynch callback function for its API. I was wondering how I can convert it to a promise. For eg:

Papa.parse(fileInput.files[0], {
    complete: function(results) {
        console.log(results);
    }
});

任何帮助将不胜感激!

推荐答案

基本模式是

Papa.parsePromise = function(file) {
  return new Promise(function(complete, error) {
    Papa.parse(file, {complete, error});
  });
};

然后

Papa.parsePromise(fileInput.files[0]) .
  then(function(results) { console.log(results); });

这篇关于如何在 PapaParse 中使用 Promises?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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