caustique / chessboard-js

A responsive mobile-first javascript chessboard library.
http://caustique.github.io/chessboard-js
MIT License
32 stars 11 forks source link

Choosing promotion piece #2

Open jackalsh opened 8 years ago

jackalsh commented 8 years ago

onMove callback receives move and should synchronously return FEN of new position. The problem is that if the move is pawn promotion, we cannot assume it's promotion to queen and need to get popup and ask user to choose it's piece. Because of asynchronous nature of javascript, onMove function can't wait for this to happen to return a value . We must use callbacks for this or promises. Unfortunately, this change will break back compatibility with old code, where your library is used. This is why I don't make pull request. The change you can see here: Link to github commit

With this change, onMove function must return a promise and resolve it in your library: Old:

function onMove(move) { // your code return FEN }

New:

function onMove(move) { return new Promise(function(resolve) { callPromotionPopup(function(promotion) { // here you will get after user chooses piece resolve(promotion) }) }) }

Thanks in advance for your feedback.