Javascript Promise函数返回未定义的

Javascript Promise函数返回未定义的,javascript,json,asynchronous,promise,fetch,Javascript,Json,Asynchronous,Promise,Fetch,救命啊,我只是想学习承诺函数。我不知道如何返回promise函数值 static getTrailer(movieId) { return fetch(`http://api.themoviedb.org/3/movie/${movieId}?api_key=###&append_to_response=videos`) .then(response => { return response.json(); })

救命啊,我只是想学习承诺函数。我不知道如何返回promise函数值

static getTrailer(movieId) {
    return fetch(`http://api.themoviedb.org/3/movie/${movieId}?api_key=###&append_to_response=videos`)
        .then(response => {
            return response.json();
        })
        .then(responseJson => {
            if (responseJson.videos.results[0]) {
                Promise.resolve(responseJson.videos.results[0].key)
                    .then(result => {
                        console.log(result);
                        return result;
                    });
            } else {
                return Promise.reject(`Trailer is not found`);
            }
        });
}
这就是我试图得到结果的地方

<p>${DataSource.getTrailer(this._movie.id).then(resultKey => {console.log("data is: " + resultKey)})}</p>
${DataSource.getTrailer(this.\u movie.id)。然后(resultKey=>{console.log(“数据是:“+resultKey”)}


但是resultKey总是返回未定义的值。如何修复此问题?

您无需使用promise再次获取密钥


static getTrailer(movieId) {
    return fetch(`http://api.themoviedb.org/3/movie/${movieId}?api_key=###&append_to_response=videos`)
        .then(response => {
            return response.json();
        })
        .then(responseJson => {
            if (responseJson.videos.results[0]) {
                result = responseJson.videos.results[0].key;
                console.log(result);
                return result;
            } else {
                return Promise.reject(`Trailer is not found`);
            }
        });
}
如果(responseJson.videos.results[0]){
则您不返回任何内容,因此承诺将解析为未定义

你为什么一开始就要用
Promise.resolve

摆脱无意义的额外承诺,返回要解析
的值,然后返回

    .then(responseJson => {
        if (responseJson.videos.results[0]) {
            const result = responseJson.videos.results[0];
            console.log(result);
            return result;
        } else {
            return Promise.reject(`Trailer is not found`);
        }
    });

要沿着承诺链传递数据,需要
返回
(显式或隐式地从arrow函数返回)

就在这里,又好又简单

static getTrailer(movieId) {
    return fetch(`http://api.themoviedb.org/3/movie/${movieId}?api_key=###&append_to_response=videos`)
    .then(response => response.json())
    .then(responseJson => responseJson.videos.results[0].key) // an error thrown for whatever reason, will caught below.
    .catch(error => {
        // an error thrown by any of the three preceding stages will end up here
        throw new Error(`Trailer is not found`); // throwing is less expensive than returning Promise.reject()
    });
}

那么,如何获取键值呢?请参见上面的代码。
key
值未分配给result,您也可以在控制台中看到它。不要忘记在
DataSource.getTrailer()…
表达式中使用
.catch()
。否则您将以“未处理的承诺拒绝”告终错误。谢谢,但是如何在自定义元素中获取密钥?下面是示例代码{/*应该是密钥*/}“>承诺无法执行或返回结果;以及您的
${……}

表达式需要结果。就目前情况而言,它是一个承诺。你必须使用完全不同的技术。如果不忽略一般代码体系结构,很难给出建议。最好还是保留这个问题,然后问另一个题为“如何在另一个字符串中包含异步派生字符串”的问题?“据我所知,这正是你们努力要做的。