在解析promise之前执行Redux Async Action

时间:2017-03-20 08:58:50

标签: javascript reactjs promise react-redux ipfs

使用以下代码时,即使在解析promise之前,Async操作“FILE_UPLOAD_SUCCESS”也会发出错误,因此操作有效内容将返回undefined。关于如何解决它的任何想法???

行动创作者

export const upload = (file) => {
    return (dispatch)=>{
        dispatch({type:"FILE_UPLOAD_START"})
            return new Promise.resolve(()=>{
               uploadDoc(file).then((hash)=>dispatch({type:"FILE_UPLOAD_SUCCESS",payload:hash}))
                }).catch((err)=>{
                    dispatch({type:"FILE_UPLOAD_ERROR",payload:err})
                })
    }
}

uploadDoc

export const uploadDoc = (file) =>{
    return new Promise((resolve,reject)=>{
        var reader = new FileReader();
        reader.readAsArrayBuffer(file[0]);
        reader.onloadend = (evt) =>{
            resolve(reader);
        }
        }).then((reader)=>{
            return Promise.resolve(()=>{
                const buffer = Buffer.from(reader.result);
                ipfs.add(buffer)
                    .then((res)=>{
                        return res[0].hash;
                })
            })
        })
}

0 个答案:

没有答案